/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package downloader.IHM;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Julien
 */
public class AddURLDialogBox extends JDialog implements ActionListener, WindowListener{

    private JTextField urlTxtZone = new JTextField();
    private JButton okButton = new JButton("Download");
    private JButton cancelButton = new JButton("Cancel");
    private MainWindows myMainWindows;

    @SuppressWarnings("LeakingThisInConstructor")
    public AddURLDialogBox(MainWindows myMainWindows){
        this.myMainWindows = myMainWindows;
        this.setVisible(false);
        this.setLayout(new BorderLayout());
        this.setSize(600,100);
        okButton.addActionListener(this);
        cancelButton.addActionListener(this);
        this.setTitle("New download");
        this.add(urlTxtZone, BorderLayout.NORTH);
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        this.add(buttonPanel, BorderLayout.SOUTH);
        this.setLocation(200, 200);
    }

    public void getNewUrl(){
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        JButton temp = (JButton) e.getSource();
        if (temp.getText().equalsIgnoreCase("Cancel")){
            urlTxtZone.setText("");
            this.setVisible(false);
        }
        if (temp.getText().equalsIgnoreCase("Download")){
            if (urlTxtZone.getText().isEmpty()){
                System.out.println("no url found");
                this.setVisible(false);
            }
            else {
                myMainWindows.newDownload(urlTxtZone.getText());
                urlTxtZone.setText("");
                this.setVisible(false);
            }
        }
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

}
